

$(document).ready(function() {
	
	//getWonderfulArticle();
		// 加载flag
    var loading = false;
    // 最多可加载的条目
    var maxItems = 100;
    // 当前页码
   var currentPage =1;
    
    //一页的数量
    var onePageNum = 4;

    function addItems(currentPage, onePageNum) {
    	loading = true;
        var getArticleURL = getContextPath()+"/article/getwonderfularticle";

        $.post(getArticleURL, {'currentPage': currentPage,'onePageNum':onePageNum}, function(data, textStatus, xhr) {
            /*optional stuff to do after success */

                    var obj=eval("("+data+")");
                    var page = obj.page;  //page对象
                    var article = obj.article; //article 列表
                    console.log(page);
                    maxItems = page.totalCount;

                    var tempHtml =""
                    console.log('article.length:'+article.length)
        for(var i=0;i<article.length;i++){
            var articleContent = article[i].articleContent.replace(/<[^>]+>/g,"");
            var articleTime =getLocalTime(article[i].articleTime) ;
            console.log(i)
            tempHtml='<div class="card" data="'+article[i].articleId+'"><div class="card-header">'+article[i].articleName+'</div><div class="card-content"><div class="list-block media-list"><ul><li class="item-content"><div class="item-media"><img src="'+"/file/image/"+article[i].articleCover+'" width="44"></div><div class="item-inner"><div class="item-title-row"></div><div class="item-subtitle">'+articleContent+'</div></div></li></ul></div></div><div class="card-footer"><span>'+articleTime+'</span> <span>点击量 '+article[i].articleClick+'</span></div></div>';
            $('#article-item').append(tempHtml);
        }
                    
                    loading = false;

        });

        }
        //预先加载
    addItems(currentPage, onePageNum);

    // 上次加载的序号

    var lastIndex = 8;
	// 下滑屏幕自动进行分页搜索
	$(document).on('click', '.preloader', function() {
		if (loading)
			return;
		currentPage=currentPage+1;
		addItems(currentPage, onePageNum);
	});
	
	//进入文章详情
	$(document).on('click', '.card', function() {
		var articleId = $(this).attr('data');
		// window.location.href=getContextPath()+'/article/showarticle?articleId='+articleId;
		 window.open(getContextPath()+'/article/showarticle?articleId='+articleId);
            
		
	});
	
	
	
	
	
	
	
	
	
});


function simpleArticle(e){
	
}

/**
 * 获取点击量高的文章
 */
function getWonderfulArticle(){
	var getWonderfulArticleURL=getContextPath()+"/article/getwonderfularticle";

	$.post(getWonderfulArticleURL, {'currentPage':currentPage,'onePageNum':onePageNum}, function(data, textStatus, xhr) {
		/*optional stuff to do after success */
		
		//js不支持  data.page.currentPage
		console.log(data);
		var obj=eval("("+data+")");
		var page = obj.page;  //page对象
		var article = obj.article; //article 列表
		$('#currentPage').html("当前页:"+page.currentPage);
		$('#totalCount').html("总记录数："+page.totalCount);
		$('#totalPage').html("总页数:"+page.totalPage);
		var tempHtml =""
			totalPage = page.totalPage;
		for(var i=0;i<article.length;i++){
			var articleContent = article[i].articleContent.replace(/<[^>]+>/g,"");
			var articleTime =getLocalTime(article[i].articleTime) ;
			console.log(articleContent)
			tempHtml+='<div class="card"><div class="card-header">'+article[i].articleName+'</div><div class="card-content"><div class="list-block media-list"><ul><li class="item-content"><div class="item-media"><img src="'+"/file/image/"+article[i].articleCover+'" width="44"></div><div class="item-inner"><div class="item-title-row"></div><div class="item-subtitle">'+articleContent+'</div></div></li></ul></div></div><div class="card-footer"><span>'+articleTime+'</span> <span>点击量 '+article[i].articleClick+'</span></div></div>';
			$('#article-item').append(tempHtml);
		}
		
		//$('#article-item').html(tempHtml);
		
	});
	
}



function dateFtt(fmt,date)   
{ //author: meizz   
  var o = {   
    "M+" : date.getMonth()+1,                 //月份   
    "d+" : date.getDate(),                    //日   
    "h+" : date.getHours(),                   //小时   
    "m+" : date.getMinutes(),                 //分   
    "s+" : date.getSeconds(),                 //秒   
    "q+" : Math.floor((date.getMonth()+3)/3), //季度   
    "S"  : date.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
}

function getLocalTime(nS) {  
	var crtTime = new Date(nS);
	 return top.dateFtt("yyyy-MM-dd hh:mm:ss",crtTime);
}



function getContextPath() { //获取根路径

	var pathName = document.location.pathname;
	var index = pathName.substr(1).indexOf("/");
	var result = pathName.substr(0, index + 1);
	return result;
}